//
//  UIViewController+Keyboard.m
//  YOHOPay
//
//  Created by chuanshuangzhang  on 8/31/15.
//  Copyright (c) 2015 chuanshuangzhang. All rights reserved.
//

#import "UIViewController+Keyboard.h"

@implementation UIViewController (Keyboard)

- (void)registerKeyboardNotificationWithView:(UIView *)view
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShown:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(keyboardDidHidden:) name:UIKeyboardWillHideNotification object:nil];
    UIButton *button = [[UIButton alloc]initWithFrame:self.view.bounds];
    [button addTarget:self action:@selector(resignKeyboard:) forControlEvents:UIControlEventTouchDown];
    [view addSubview:button];
}
- (void)registerKeyboardNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShown:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(keyboardDidHidden:) name:UIKeyboardWillHideNotification object:nil];
    UIButton *button = [[UIButton alloc]initWithFrame:self.view.bounds];
    [button addTarget:self action:@selector(resignKeyboard:) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:button];
    [self.view sendSubviewToBack:button];
}
- (void)addKeyboardButton{
    UIButton *button = [[UIButton alloc]initWithFrame:self.view.bounds];
    [button addTarget:self action:@selector(resignKeyboard:) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:button];
    [self.view sendSubviewToBack:button];
}
-(void)keyboardDidShown:(NSNotification *)notification{
    NSDictionary *info = [notification userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    
    for (UIView *view in self.view.subviews) {
        if([view isKindOfClass:[UITextField class]]){
            if([view isFirstResponder]){
                [self adjustViewLocation:view keyboardSize:keyboardSize];
                return;
            }
        }else if([view isKindOfClass:[UIScrollView class]]){
            for (UIView *subView in view.subviews) {
                if([subView isKindOfClass:[UITextField class]]){
                    if([subView isFirstResponder]){
                        [self adjustViewLocation:subView keyboardSize:keyboardSize];
                        return;
                    }
                }
            }
        }
    }
}

- (void)adjustViewLocation:(UIView *)view keyboardSize:(CGSize)keyboardSize
{
    CGRect frame = view.frame;
    frame = [view.superview convertRect:frame toView:self.view];
    CGFloat moveDistance = self.view.frame.size.height - (frame.origin.y+frame.size.height)-keyboardSize.height-20;
    if (moveDistance<=0){
        [UIView animateWithDuration:0.2 animations:^{
            self.view.center = CGPointMake(self.view.center.x, 64 + self.view.frame.size.height * 0.5 + moveDistance);
        }];
    }else{
        [UIView animateWithDuration:0.2 animations:^{
            self.view.center = CGPointMake(self.view.center.x,64+  self.view.frame.size.height*0.5);
        }];
    }
}
//键盘消失通知
-(void)keyboardDidHidden:(NSNotification *)notification{
    [UIView animateWithDuration:0.2 animations:^{
        self.view.center = CGPointMake(self.view.center.x,64+ self.view.frame.size.height*0.5);
    }];
}
- (IBAction)resignKeyboard:(id)sender
{
  [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
@end
